<?php

include_once dirname(__FILE__) . "/../db.php";
include_once dirname(__FILE__) . "/../log.php";

Log::init("Db_test");

class Db_test extends PHPUnit_Framework_TestCase
{
    public function test_init_with_invalid_conf()
    {
        global $err_code;

        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file format");

        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($err_code == ec_db_conf_error);
        $db->close();

        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    public function test_execute_select_failed()
    {
        global $err_code;

        $db = new Db();
        
        $ret = $db->init();
        $this->assertTrue($ret);

        $sql = "select * from not_exist_table";
        $ret = $db->execute($sql);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_db_error);
        $db->close();
    }
    
    public function test_execute_insert_failed()
    {
        global $err_code;

        $db = new Db();
        
        $ret = $db->init();
        $this->assertTrue($ret);

        $sql = "insert into not_exist_table values(1, 1)";
        $ret = $db->execute($sql);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_db_error);
        $db->close();
    }
    
    public function test_execute_select_succ()
    {
        global $err_code;

        $db = new Db();
        
        $ret = $db->init();
        $this->assertTrue($ret);

        $sql = "select * from user_table";
        $ret = $db->execute($sql);
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($err_code == ec_ok);
        $db->close();
    }
    
    public function test_execute_update_succ()
    {
        global $err_code;

        $db = new Db();
        
        $ret = $db->init();
        $this->assertTrue($ret);

        $sql = "update user_table set city='深圳' where open_id='abc'";
        $ret = $db->execute($sql);
        $this->assertTrue($ret == 0);
        $this->assertTrue($err_code == ec_ok);
        $db->close();
    }
    public function test_get_top_db_with_invalid_conf()
    {
        global $err_code;

        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file format");

        $db = new Db();
        $ret = $db->get_top_dish();
        $this->assertTrue($err_code == ec_conf_error);

        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_top_dish_with_db_error()
    {
        global $err_code;
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "dish.list.top = 3");

        $db = new Db();
        $ret = $db->init();
        $this->assertFalse($ret);
        $ret = $db->get_top_dish();
        $this->assertTrue($err_code == ec_db_conf_error);

        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }

    public function test_get_top_dish_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $ret = $db->get_top_dish();
        $this->assertTrue(count($ret) > 0);
        $this->assertTrue($err_code == ec_ok);
    }

    public function test_get_home_dish_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array();
        $ret = $db->get_home_dish($arg);
        $this->assertTrue($ret == false);
    }
    
    public function test_get_home_dish_with_conf_error()
    {
        global $err_code;
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file");

        $db = new Db();
        $ret = $db->init();
        $this->assertFalse($ret);
        $arg = array(
            "province" => "广东",
            "city" => "深圳",
        );
        $ret = $db->get_home_dish($arg);
        $this->assertTrue($err_code == ec_conf_error);

        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_home_dish_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "province" => "广东",
            "city" => "深圳",
        );
        $ret = $db->get_home_dish($arg);
        //print_r($ret);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }

    public function test_is_new_user_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array();
        $ret = $db->is_new_user($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_is_new_user_with_not_found_error()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "xxxxxxxxxx11111123",
        );
        $ret = $db->is_new_user($arg);
        $this->assertTrue($ret == array());
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_get_random_dish_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_random_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_random_dish_with_conf_error()
    {
        global $err_code;
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file");
        
        $arg = array(
            0 => array(
                "dish_id" => 3,
            ),
            1 => array(
                "dish_id" => 4,
            ),
        );
        $ret = $db->get_random_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_conf_error);
        
        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_random_dish_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $arg = array(
            0 => array(
                "dish_id" => 3,
            ),
            1 => array(
                "dish_id" => 4,
            ),
        );
        $ret = $db->get_random_dish($arg);
        $this->assertTrue(count($ret) > 0);
        //$this->assertTrue($err_code == ec_dish_not_enough_error);
    }
    
    public function test_get_eaten_dish_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_eaten_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    public function test_get_eaten_dish_with_conf_error()
    {
        global $err_code;

        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file");
        
        $arg = array(
            "open_id" => "abc",
        );
        $ret = $db->get_eaten_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_conf_error);
        
        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_eaten_dish_ok()
    {
        global $err_code;

        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $arg = array(
            "open_id" => "abc",
        );
        $ret = $db->get_eaten_dish($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
        //print_r($ret);
    }
    
    public function test_get_friend_dish_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_friend_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_friend_dish_with_conf_error()
    {
        global $err_code;

        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file");
        
        $arg = array(
            "open_id" => "abc",
        );
        $ret = $db->get_friend_dish($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_conf_error);
        
        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_friend_dish_ok()
    {
        global $err_code;

        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        
        $arg = array(
            "open_id" => "abc",
        );
        $ret = $db->get_friend_dish($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
        //print_r($ret);
    }
    public function test_update_dish_vote_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->update_dish_vote($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_update_dish_vote_with_no_family_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(10),
            "open_id" => "abc",
        );
        $ret = $db->update_dish_vote($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_update_favor_error);
    }
    public function test_update_dish_vote_with_old_user_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(29),
            "open_id" => "abc",
        );
        $ret = $db->update_dish_vote($arg);
        $this->assertTrue($ret);
        $this->assertTrue($err_code == ec_ok);
    }
    public function test_get_similar_dish_family_user_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_similar_dish_family_user($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_similar_dish_family_user_with_no_family_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(10),
            "open_id" => "abc",
        );
        $ret = $db->get_similar_dish_family_user($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_get_similar_dish_family_user_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(29),
            "open_id" => "abc",
        );
        $ret = $db->get_similar_dish_family_user($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }

    public function test_get_similar_dish_user_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_similar_dish_user($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_similar_dish_user_with_no_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(10),
            "open_id" => "abc",
            "num" => 1,
        );
        $ret = $db->get_similar_dish_user($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_get_similar_dish_user_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(29),
            "open_id" => "abc",
            "num" => 1,
        );
        $ret = $db->get_similar_dish_user($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_float_once_buddy_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $list = array();
        $ret = $db->float_once_buddy($list, $arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_float_once_buddy_no_buddy_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "abc",
            "num" => 1,
        );
        $list = array(
            "xyz" => array(
                "open_id" => "xyz",
            ),
            "rst" => array(
                "open_id" => "rst",
            ),
        );
        $ret = $db->float_once_buddy($list, $arg);
        $this->assertTrue($ret["xyz"] == $list["xyz"]);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_float_once_buddy_one_buddy_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "abc",
            "num" => 1,
        );
        $list = array(
            "xyz" => array(
                "open_id" => "xyz",
            ),
            "def" => array(
                "open_id" => "def",
            ),
        );
        $ret = $db->float_once_buddy($list, $arg);
        $ret = array_keys($ret);
        $list = array_keys($list);
        $this->assertTrue($ret[0] == $list[1]);
        $this->assertTrue($err_code == ec_ok);
    }
    /*
    public function test_get_rest_vote_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_rest_vote($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_rest_vote_with_no_rand_rest()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "dish_list" => "66",
            "num" => 1,
        );
        $ret = $db->get_rest_vote($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_get_rest_vote_with_one_rand_rest()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "dish_list" => "29",
            "num" => 1,
        );
        $ret = $db->get_rest_vote($arg);
        $this->assertTrue(count($ret) >= 0);
        $this->assertTrue($err_code == ec_ok);
       // print_r($ret);
    }
    public function test_get_random_user_invalid_args()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = 10;
        $ret = $db->get_random_user($arg);
        $this->assertTrue($ret == false);
        //print_r($err_code);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_random_user_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "abc",
            "num" => 1,
        );
        $ret = $db->get_random_user($arg);
        $this->assertTrue(count($ret) > 0);
        //print_r($ret);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_get_user_vote_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array();
        $rest_id = 1;
        $ret = $db->get_user_vote($arg, $rest_id);
        $this->assertTrue(count($ret) > 0);
        //print_r($ret);
        $this->assertTrue($err_code == ec_param_error);
    }
    public function test_get_user_vote_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "abc",
        );
        $rest_id = 1;
        $ret = $db->get_user_vote($arg, $rest_id);
        $this->assertTrue($ret == 1);
        //print_r($ret);
        $this->assertTrue($err_code == ec_ok);
    }
    public function test_add_similar_tag_with_invalid_arg()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array();
        $list = array();
        $ret = $db->add_similar_tag($arg, $list);
        $this->assertTrue($ret == false);
        //print_r($ret);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_add_similar_tag_ok()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "open_id" => "abc",
        );
        $list = array(
            0 => array(
                "open_id" => "def",
            ),
        );
        $ret = $db->add_similar_tag($arg, $list);
        $this->assertTrue(array_key_exists("sim", $ret[0]));
        //print_r($ret);
    }
    
    /*
    public function test_update_dish_vote_with_new_user_found()
    {
        global $err_code;
        
        $db = new Db();
        $ret = $db->init();
        $this->assertTrue($ret);
        $arg = array(
            "selected" => array(29),
            "open_id" => "abc",
        );
        $ret = $db->update_dish_vote($arg);
        $this->assertTrue($ret);
        $this->assertTrue($err_code == ec_ok);
    }
/*
 */
}
?>
